#ifndef SERIAL_DEVICE_HPP
#define SERIAL_DEVICE_HPP

#include "global_para.hpp"

class LinuxSerialDevice 
{
public:
  static const int BUFFER_SIZE = 2048;

public:
  LinuxSerialDevice(const char* device, uint32_t baudrate);
  ~LinuxSerialDevice();

  void init();
  void initOdd();//
  bool getDeviceStatus();

  void setBaudrate(uint32_t baudrate);
  void setDevice(const char* device);

  int checkBaudRate(uint8_t (&buf)[BUFFER_SIZE])
  {
    return _checkBaudRate(buf);
  }
  int setSerialPureTimedRead();
  int unsetSerialPureTimedRead();
  int serialRead(uint8_t* buf, int len);

  size_t send(const uint8_t* buf, size_t len);
  size_t readall(uint8_t* buf, size_t maxlen);


private:
  const char* m_device;
  uint32_t    m_baudrate;

  int    m_serial_fd;
  fd_set m_serial_fd_set;
  bool   deviceStatus;

  bool _serialOpen(const char* dev);
  bool _serialClose();
  bool _serialFlush();
  bool _serialConfig(int baudrate, char data_bits, char parity_bits,
                     char stop_bits, bool testForData = false);

  int _serialStart(const char* dev_name, int baud_rate);
  int _serialStartOdd(const char* dev_name, int baud_rate);//
  int _serialWrite(const uint8_t* buf, int len);
  int _serialRead(uint8_t* buf, int len);

  int _checkBaudRate(uint8_t (&buf)[BUFFER_SIZE]);
};


bool NX_serial_init(LinuxSerialDevice* radio);
void NX_serial_read_raw_data(LinuxSerialDevice* radio, circleBuf_t* temp);
void NX_serial_read_SBUS_raw_data(LinuxSerialDevice* serial, circleBuf_t* temp);
void NX_serial_read_flight_control_raw_data(LinuxSerialDevice* radio, circleBuf_t* temp);
void NX_analyse_landpad_RTK_data(circleBuf_t* temp, uint8_t* data);    
void NX_analyse_groundbase_data(circleBuf_t* temp, uint8_t* data);
void NX_analyse_SBUS_data(circleBuf_t* temp, uint8_t* data);
void NX_analyse_flight_control_data(circleBuf_t* temp, uint8_t* data);
void NX_analyse_camera_data(circleBuf_t* temp, uint8_t* data);
#endif